package com.jp.knowledge.service;

import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.google.gson.JsonObject;
import com.jp.knowledge.broad.LocalBroadCast;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.logic.UploadLogic;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.UserData;
import com.jp.knowledge.my.model.SettingUserDataModel;
import com.jp.knowledge.util.HeadIconUploader;
import com.jp.knowledge.util.MultipleImageUploader;
import com.jp.knowledge.util.ResultSubscriber;

import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/4/6.
 */

public class SaveUserDataService extends BaseService implements MultipleImageUploader.ImageUploadCallBack, ResultSubscriber.OnResultListener {

    public static final String SETTING_USER_DATA_MODEL = "settingUserDataModel";
    private static final int UPDATE_USER_INFO = 0x01;

    private SettingUserDataModel settingUserDataModel;
    private UserData userData;
    private HeadIconUploader headIconUploader;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        userData = application.getUserData();
        headIconUploader = new HeadIconUploader(this);
        headIconUploader.setImageUploadCallBack(this);
        settingUserDataModel = (SettingUserDataModel) intent.getSerializableExtra(SETTING_USER_DATA_MODEL);

        if(settingUserDataModel == null || userData == null) {
            stopSelf();
        } else{
            if(settingUserDataModel.getPortrait() != null
                    && !settingUserDataModel.getPortrait().equals(userData.getPortrait())){
                MultipleImageUploader.ImageBean imageBean = new MultipleImageUploader.ImageBean();
                imageBean.setPath(settingUserDataModel.getPortrait());
                String key = String.format("user/info/icon/%s/%s", userData.getUuid(), UploadLogic.getTime());
                imageBean.setKey(key);
                headIconUploader.addImage(imageBean);
                headIconUploader.uploadImages();
            } else {
                commitData();
            }
            settingUserDataModel.updateSettingDataToUserData(userData);
//            LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(LocalBroadCast.USER_DATA_CHANGE));
        }
        return super.onStartCommand(intent, flags, startId);
    }

    /* 提交用户数据 */
    private void commitData(){
        JsonObject param = settingUserDataModel.toJsonObject(userData);
        if(param != null){
            HttpHelper.getInstance(this).updateUserInfo(param, UPDATE_USER_INFO, this);
        } else{
            stopSelf();
        }
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {
        settingUserDataModel.updateSettingDataToUserData(userData);
        String portrait = userData.getPortrait();
        if(!portrait.contains("pic.jp580.com") && !portrait.contains("team_circle_upload"))
            userData.setPortrait("http://pic.jp580.com/" + portrait);
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(LocalBroadCast.USER_DATA_CHANGE));
        stopSelf();
    }
    @Override
    public void onError(int requestType) {
        onCompleted(requestType);
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0)
            onError(requestType);
    }

    /* 头像上传回调 */
    @Override
    public void imageCompressFail(String key) {
        imageUploadSuccess(key, userData.getPortrait());
    }
    @Override
    public void imageCompressSuccess(String key) {}
    @Override
    public void imageCompressComplete() {}
    @Override
    public void imageUploadFail(String key) {
        imageUploadSuccess(key, userData.getPortrait());
    }
    @Override
    public void imageUploadSuccess(String key, String resultKey) {
        settingUserDataModel.setPortrait(resultKey);
        commitData();
    }
    @Override
    public void imageUploadComplete(List<String> resultKeys) {}
}
